package site.laodengtou.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import jxl.Sheet;
import jxl.Workbook;
import site.laodengtou.pojo.Admin;
import site.laodengtou.pojo.AdminVo;
import site.laodengtou.pojo.StuVo;
import site.laodengtou.pojo.Student;
import site.laodengtou.pojo.TeaVo;
import site.laodengtou.pojo.Teacher;
import site.laodengtou.service.AdminService;
import site.laodengtou.util.Msg;

@Controller
public class AdminController {
	
    
	@Autowired
	AdminService adminService;
	
	
	@RequestMapping(value="/addTeacher",method=RequestMethod.POST)
	public void importFile(MultipartFile file) throws Exception {
		System.out.println("开始导入老师信息");
	    // 1.获取用户上传的文件
	    Workbook workbook = Workbook.getWorkbook(file.getInputStream());
	    // 2.获取工作簿sheet
	    Sheet sheet = workbook.getSheet(0);
	    // 3.获取总行数
	    int rows = sheet.getRows();
	    for (int i = 1; i < rows; i++) {
	        Teacher tea = new Teacher();
	        tea.setUsername(sheet.getCell(0, i).getContents());
	        tea.setPassword(sheet.getCell(1, i).getContents());
	        tea.setCategory(sheet.getCell(2, i).getContents());
	        // 4.添加到数据库中
	        adminService.addTeacher(tea);
	    }
	}
	
	//教师登录
	@RequestMapping(value="/adminlogin",method=RequestMethod.POST)
	public String adminlogin(Admin admin) throws Exception {
	    
		List<Admin> list = adminService.adminlogin(admin);
		if(list.size()==1) {
			return "indexAdmin";
		}else {
			return "loginAdmin";
		}
	}
	
	//修改密码
	@RequestMapping(value="/updatePassword",method=RequestMethod.POST)
	public Msg updatePassword(AdminVo adminVo) throws Exception {
	    
		System.out.println(adminVo);
		if(adminVo.getShenfen().equals("管理员")) {
			System.out.println("1");
			adminService.updatePassword(adminVo);
		}
		if(adminVo.getShenfen().equals("老师")) {
			System.out.println("2");
			TeaVo tea = new TeaVo();
			tea.setUsername(adminVo.getUsername());
			tea.setOldPass(adminVo.getOldPass());
			tea.setPassword(adminVo.getPassword());
			adminService.updatePasswordfortea(tea);
		}
		if(adminVo.getShenfen().equals("学生")) {
			System.out.println("3");
			StuVo stu = new StuVo();
			stu.setUsername(adminVo.getUsername());
			stu.setOldPass(adminVo.getOldPass());
			stu.setPassword(adminVo.getPassword());
			adminService.updatePasswordforStu(stu);
		}
		//adminService.updatePassword(adminVo);
		return Msg.success();
	}
}
